package com.fet.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class ConnectionFactory {
	
	private static ConnectionFactory connectionFactory;
	private Properties properties;
	private Connection conn;
	
	static{
		connectionFactory = new ConnectionFactory()	;
	}
	
	private ConnectionFactory(){
	}
	
	public static ConnectionFactory getConnectionFactory(){
		return connectionFactory;
	}
	
	public void setProperties(Properties properties){
		this.properties = properties;
	}

	public Connection getConnection() throws Exception{
		if(conn != null && !conn.isClosed())
			return conn;
		Class.forName(properties.getProperty("driver"));
		String url = properties.getProperty("url");
		String name = properties.getProperty("userName");
		String password = properties.getProperty("password");
		conn = DriverManager.getConnection(url,name,password);
		return conn;
	}
	
	public void closeConnection(){
		if(conn != null)
			try{
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
}
